import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

/*
 * UI.java
 * Author: Russell Sowell
 * Last updated: 2/4/2012
 */
public class UI{

	private static final JFrame myMainFrame = new JFrame();
	private static final JPanel myMainPanel = new JPanel();
	private static final JTextArea myKeywordArea = new JTextArea(10, 10);
	private static final JTextField myURLField = new JTextField(50);
	private static final JButton myStartButton = new JButton("Start Search");
	private static final JRadioButton singleThread = new JRadioButton("Single Threaded");
	private static final JRadioButton multiThread = new JRadioButton("Multi Threaded");
	private static int DEFAULT_MAX_PAGES_TO_BE_CRAWLED = 10;
	private UIResults myUIResults = new UIResults();
	private CrawlerApplication myCrawler;
	
	
	public UI()
	{		
			initializeFrame();
			initializeLayout();
			
			singleThread.setSelected(true);
			myStartButton.addActionListener(new ListenStart());
	}
	
	private void initializeFrame()
	{
		myMainFrame.add(myMainPanel);
		
		myMainFrame.setSize(600, 300);
		myMainFrame.setTitle("Web Crawler");
		myMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myMainFrame.setResizable(false);
		myMainFrame.setVisible(true);
	}
	
	private void initializeLayout()
	{
		GroupLayout layout = new GroupLayout(myMainPanel);
		myMainPanel.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		JScrollPane scroll = new JScrollPane (myKeywordArea, 
				   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		JLabel url = new JLabel("URL");
		JLabel keyWords = new JLabel("Key Words");
		
		
		
		ButtonGroup buttGroup = new ButtonGroup();
		buttGroup.add(singleThread);
		buttGroup.add(multiThread);
		
		
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			    .addGroup(layout.createSequentialGroup()	
			    	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			    		.addComponent(url)
			    		.addComponent(keyWords)
			    		.addComponent(singleThread)
			    		.addComponent(multiThread))
			    		
			    	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			    			.addComponent(myURLField)	
			    			.addComponent(scroll)		        
			        		.addComponent(myStartButton))));

			layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(url).addComponent(myURLField))
			    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
			    		.addComponent(keyWords).addComponent(scroll))
			    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
			    		.addComponent(singleThread)
			    		.addComponent(myStartButton))
			    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
			    		.addComponent(multiThread)));
	}
	
	
	private class ListenStart implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			//myCrawler = new CrawlerApplication(myURLField.getText(), DEFAULT_MAX_PAGES_TO_BE_CRAWLED);
			myUIResults.updateResults("Placeholder");
			
		}
		
	}
	
	
	public static void main(String[] args) {
		 
		UI gui = new UI();	
		
	}	
}
